/*
ID: cherish2
LANG: JAVA
PROG: gift1
*/

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

public class gift1 {

	public static void main(String[] args) throws IOException {

		BufferedReader reader = new BufferedReader(new FileReader("gift1.in"));
		PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(
				"gift1.out")));

		StringBuffer buffer = new StringBuffer();
		String text;
		while ((text = reader.readLine()) != null) {
			buffer.append(text + ',');
		}
		text = buffer.toString();

		StringTokenizer st = new StringTokenizer(text, ",");
		List<String> list = new ArrayList<String>();
		while (st.hasMoreTokens()) {
			list.add(st.nextToken());
		}
		
		int num = Integer.parseInt(list.get(0));
		Map<String, Integer> map = new HashMap<String, Integer>();
		for (int i = 1; i <= num; i++) {
			map.put(list.get(i), 0);
		}
		
		for (int i = num + 1; i < list.size();) {
			//person who sends gifts
			String name = list.get(i);
			int money = map.get(name);
			String[] gifts = list.get(i+1).split(" ");
			int gift = Integer.parseInt(gifts[0]);
			int friends = Integer.parseInt(gifts[1]);
			if(friends != 0){
				money = money - gift + gift % friends;
				map.put(name, money);
				
				//the friends receive gifts
				int a = i + 2;
				int get = gift / friends;
				for(int j = a; j < friends + a; j++){
					map.put(list.get(j), map.get(list.get(j)) + get);
					
				}
				i += 2 + friends;
			}else{
				i += 2;
			}
		}
		
		for(int i = 1; i <= num; i++){
			out.println(list.get(i)+" " + map.get(list.get(i)));
		}
		out.close();
		System.exit(0);
	}
}
